﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using TempCtr.Model;

namespace TempCtr.MyControl
{
    public partial class TempCard : UserControl
    {
        public Thermostat Th { get; set; }

        public TempCard(Thermostat thermostat)
        {
            InitializeComponent();
            Th = thermostat;
            UpShowFirst();
            UpShowOnline(false);
        }

        public void UpShowOnline(bool isOnline)
        {
            Action DoAction = delegate ()
            {
                if (isOnline == true)//正常
                {
                    Panel1.BackColor = Color.FromArgb(0x66,0xcc,0xff);
                }
                else
                {
                    Panel1.BackColor = Color.Gray;
                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowFirst()
        {
            Action DoAction = delegate ()
            {
                LB_ID.Text = "柜号:" + Th.Num;
                LB_Temp.Text = "XXX℃";
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowTemp(byte type)
        {
            Action DoAction = delegate ()
            {
                LB_Temp.BackColor=LB_Temp.ForeColor;
                LB_Temp.ForeColor = Panel1.BackColor;
                LB_Temp.Refresh();

                Thread.Sleep(50);
                LB_Temp.BackColor = Panel1.BackColor;
                LB_ID.Text = "柜号:" + Th.Num;
                if (System.Math.Abs(Th.Temp) >= 10)
                {
                    LB_Temp.Text = Th.Temp.ToString("00.0") + "℃";
                }
                if (System.Math.Abs(Th.Temp) < 10)
                {
                    LB_Temp.Text = Th.Temp.ToString("0.00") + "℃";
                }
                if (Th.Temp < 0)
                {
                    LB_Temp.Location = new Point(25, 70);
                }
                else
                {
                    LB_Temp.Location = new Point(35, 70);
                }

                if (type==0)//正常
                {
                    LB_Temp.ForeColor = Color.Green;                
                }
                if(type == 1)//高温
                {
                    LB_Temp.ForeColor = Color.Red;
                }
                if (type == 2)//低温
                {
                    LB_Temp.ForeColor = Color.Blue;
                }                
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }


        private void Panel1_Click(object sender, EventArgs e)
        {          
            temp_variation f1 = new temp_variation(Th);
            f1.ShowDialog();
        }
    }
}
